<template>
  <div class="user-container">
    <el-form ref="form" :inline="true" :model="form" label-width="40px">
        <el-form-item label="名称">
            <el-input v-model="form.dirName" placeholder="输入字典名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="类型">
            <el-input v-model="form.dirType" placeholder="输入字典类型" clearable></el-input>
        </el-form-item>  
        <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
    </el-form>
    <el-button icon="el-icon-plus" type="primary" @click="addCount">添加</el-button>
    <!-- <el-button icon="el-icon-edit" type="info" @click="editCount">修改</el-button>
    <el-button icon="el-icon-delete" type="warning" @click="delCount">删除</el-button> -->
    <div class="user-table">
        <el-table
        ref="multipleTable"
        :data="dirList"
        border
        stripe
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
        type="index"
        width="40">
        </el-table-column>
        <!-- <el-table-column
        type="selection"
        width="55">
        </el-table-column> -->
        <el-table-column
        prop="code"
        label="编码"
        >   
        </el-table-column>
        <el-table-column
        prop="name"
        label="名称"
        >   
        </el-table-column>
        <el-table-column
        prop="type"
        label="类型"
       >
        </el-table-column> 
        <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="editDir(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="delDir(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="user-pagination">
    <el-pagination
    background
    layout="prev, pager, next"
    :total="totalItem">
    </el-pagination>
  </div>
  <el-dialog ref="editdir" :title="dialogTitle" :visible.sync="dialogFormVisible"  center>
  <el-form :model="dialogForm" label-width="40px">
    <el-form-item label="编码">
      <el-input v-model="dialogForm.dirCode" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="名称">
      <el-input v-model="dialogForm.dirName" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="类型">
      <el-input v-model="dialogForm.dirType" auto-complete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="handleCancelSubmit">取 消</el-button>
    <el-button type="primary" @click="handleDialogSubmit('editdir')">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'dir',
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  data() {
      return {
          form:{
              dirName:'',
              dirType:''
          },
          dialogForm:{
              dirCode:'',
              dirName:'',
              dirType:''
          },
          dirList:[],
          tableData3: [{
            code: '2016-05-03',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
            }, {
            code: '2016-05-02',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
            }, {
            code: '2016-05-04',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
            }, {
            code: '2016-05-01',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
            }, {
            code: '2016-05-08',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
            }, {
            code: '2016-05-06',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
            }, {
            code: '2016-05-07',
            name: '王小虎',
            type: '上海市普陀区金沙江路 1518 弄'
        }],
        multipleSelection: [],
        dialogTitle:'添加字典',
        dialogFormVisible:false,
        page:1,
        rows:10,
        totalItem:0,
        currentDialogType:'',
        dialogTitle:'',
        currentEditId:''
      }
  },
  created() {
    this.hanleGetListAll();
  },
  methods:{
      search() {
        this.$store.dispatch('FindDictByDict',{name: this.form.dirName,type: this.form.dirType}).then(res=>{
          if(res.result === 'success'){
            let _data = res.data;
            
            // 暂时没有数据
          }
        }).catch(err=>{
          console.log(err)
          this.$message({
            type: 'warning',
            message: '没有相关结果'
          }); 
        })
      },
      handleSelectionChange() {

      },
      addCount() {
          this.dialogFormVisible = true;
          this.dialogTitle = '添加字典';
          this.currentDialogType = 'add';
      },
      editDir(index,data) {
          this.dialogFormVisible = true;
          this.dialogTitle = '编辑字典'
          this.currentDialogType = 'edit';
          this.currentEditId = data.id;
          this.$store.dispatch('GetSingleDict', {id: data.id}).then(res=>{
            let data = res.data;
            if(res.result === 'success'){
              this.dialogForm.dirName = data.name;
              this.dialogForm.dirCode = data.code;
              this.dialogForm.dirType = data.type;
              // this.dialogForm.userPwd = data.password;
            }
          })
      },
      delDir(index,data) {
        this.$confirm('确认删除该角色？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('DeleteDict',{id: data.id}).then(res=>{
            this.hanleGetListAll();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(err=>{
            console.log(err);
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      handleDialogSubmit(editdir){
        console.log(this.$refs)
        // this.$refs.editdir.validate((valid)=>{
          // if(valid){
            if(this.currentDialogType === 'add'){
              this.$store.dispatch('AddDict',{
                name: this.dialogForm.dirName,
                type: this.dialogForm.dirType,
                code: this.dialogForm.dirCode,
              }).then(res=>{
                this.hanleGetListAll();
                this.clearDialogForm();
              }).catch(err=>{
                console.log(err)
              })
            }else {
              this.$store.dispatch('UpdateDict',{
                id: this.currentEditId,
                name: this.dialogForm.dirName,
                type: this.dialogForm.dirType,
                code: this.dialogForm.dirCode,
              }).then(res=>{
                this.hanleGetListAll();
                this.clearDialogForm();
              }).catch(err=>{
                console.log(err)
              })
            }
            
          // }
        // })
      },
      hanleGetListAll(){
        this.$store.dispatch('GetDictList',{page: this.page,rows: this.rows}).then(res => {
          console.log(res)
          if(res.result === 'success'){
            this.totalItem = res.data.total;
            this.dirList = res.data.rows;
          }
        });
      },
      handleCancelSubmit(){
        this.clearDialogForm();
      },
      clearDialogForm(){
        this.dialogFormVisible = false;
        this.dialogTitle = '';
        this.currentEditId = "";
        this.dialogForm.dirName = '';    
        this.dialogForm.dirType = '';
        this.dialogForm.dirCode = '';
        this.currentDialogType = '';
      }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.user {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
  &-pagination {
      margin-top:50px;
  }
  &-table {
      margin-top:20px;
  }
}
</style>